<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的-邮件发送功能" tabindex="-1"><a class="header-anchor" href="#laravel-的-邮件发送功能"><span>Laravel 的 邮件发送功能</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#driver-prerequisites">驱动前提</a></li>
</ul>
</li>
<li><a href="#generating-mailables">生成 Mailable</a></li>
<li><a href="#writing-mailables">编写 Mailable</a>
<ul>
<li><a href="#configuring-the-sender">配置发送者</a></li>
<li><a href="#configuring-the-view">配置视图</a></li>
<li><a href="#view-data">视图数据</a></li>
<li><a href="#attachments">附件</a></li>
<li><a href="#inline-attachments">内部附件</a></li>
<li><a href="#customizing-the-swiftmailer-message">自定义 SwiftMailer 消息</a></li>
</ul>
</li>
<li><a href="#markdown-mailables">Markdown 格式的邮件</a>
<ul>
<li><a href="#generating-markdown-mailables">生成 Markdown 格式的邮件</a></li>
<li><a href="#writing-markdown-messages">编写 Markdown 格式的邮件</a></li>
<li><a href="#customizing-the-components">自定义组件</a></li>
</ul>
</li>
<li><a href="#previewing-mailables-in-the-browser">在浏览器中预览邮件</a></li>
<li><a href="#sending-mail">发送邮件</a>
<ul>
<li><a href="#queueing-mail">队列邮件</a></li>
</ul>
</li>
<li><a href="#mail-and-local-development">邮件与本地开发</a></li>
<li><a href="#events">事件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 基于 <a href="http://swiftmailer.org/" target="_blank" rel="noopener noreferrer">SwiftMailer</a> 函数库提供了一套干净、简洁的邮件 API ，Laravel 为 SMTP 、Mailgun 、SparkPost 、 Amazon SES 、 PHP 的 <code v-pre>mail</code> 函数及 <code v-pre>sendmail</code> 提供驱动，让你可以快速从本地或云端服务自由地发送邮件。</p>
<h3 id="驱动前提" tabindex="-1"><a class="header-anchor" href="#驱动前提"><span>驱动前提</span></a></h3>
<p>基于 API 的驱动，例如 Mailgun 和 SparkPost 通常比 SMTP 服务器更简单快速。如果可能，你应该尽可能使用这些驱动。所有的 API 驱动都需要 Guzzle HTTP 函数库，你可以使用 Composer 包管理器安装它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> guzzlehttp<span class="token operator">/</span>guzzle</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="mailgun-驱动" tabindex="-1"><a class="header-anchor" href="#mailgun-驱动"><span>Mailgun 驱动</span></a></h4>
<p>要使用 Mailgun 驱动，首先必须安装 Guzzle，之后将 <code v-pre>config/mail.php</code> 配置文件中的 <code v-pre>driver</code> 选项设置为 <code v-pre>mailgun</code>。接下来，确认 <code v-pre>config/services.php</code> 配置文件包含以下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailgun'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'domain'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-mailgun-domain'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-mailgun-key'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="sparkpost-驱动" tabindex="-1"><a class="header-anchor" href="#sparkpost-驱动"><span>SparkPost 驱动</span></a></h4>
<p>要使用 SparkPost 驱动，首先必须安装 Guzzle，之后将 <code v-pre>config/mail.php</code> 配置文件中的 <code v-pre>driver</code> 选项设置为 <code v-pre>sparkpost</code>。接下来，确认 <code v-pre>config/services.php</code> 配置文件包含以下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'sparkpost'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-sparkpost-key'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ses-驱动" tabindex="-1"><a class="header-anchor" href="#ses-驱动"><span>SES 驱动</span></a></h4>
<p>要使用 Amazon SES 驱动，必须安装 PHP 的 Amazon AWS SDK。你可以在 <code v-pre>composer.json</code> 文件的 <code v-pre>require</code> 段落加入下面这一行并运行 <code v-pre>composer update</code> 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string double-quoted-string">"aws/aws-sdk-php"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"~3.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，将 <code v-pre>config/mail.php</code> 配置文件中的 <code v-pre>driver</code> 设置为 <code v-pre>ses</code>。然后确认 <code v-pre>config/services.php</code> 配置文件包含下列选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'ses'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-ses-key'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-ses-secret'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'region'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'ses-region'</span><span class="token punctuation">,</span>  <span class="token comment">// e.g. us-east-1</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="生成-mailable" tabindex="-1"><a class="header-anchor" href="#生成-mailable"><span>生成 Mailable</span></a></h2>
<p>在 Laravel 中，每种类型的邮件都代表一个「Mailable」对象。这些对象存储在 <code v-pre>app/Mail</code> 目录中。如果在你的应用中没有看见这个目录，别担心，在首次使用 <code v-pre>make:mail</code> 命令创建 Mailable 类时这个目录会被创建，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>mail OrderShipped</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="编写-mailable" tabindex="-1"><a class="header-anchor" href="#编写-mailable"><span>编写 Mailable</span></a></h2>
<p>所有的 「Mailable」类都在其 <code v-pre>build</code> 方法中完成配置。在这个方法里，你可以调用其他各种方法，如 <code v-pre>from</code> 、 <code v-pre>subject</code> 、 <code v-pre>view</code> 和 <code v-pre>attach</code> 来配置完成邮件的详情。</p>
<h3 id="配置发送者" tabindex="-1"><a class="header-anchor" href="#配置发送者"><span>配置发送者</span></a></h3>
<h4 id="使用-from-方法" tabindex="-1"><a class="header-anchor" href="#使用-from-方法"><span>使用 <code v-pre>from</code> 方法</span></a></h4>
<p>首先，演示配置邮件的发送者，也就是邮件的参数 「from」，既谁发送了邮件。有两种方法配置发送者。第一种是你可以在 <code v-pre>build</code> 方法中使用 <code v-pre>from</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Build the message.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="使用一个全局-from-地址" tabindex="-1"><a class="header-anchor" href="#使用一个全局-from-地址"><span>使用一个全局 <code v-pre>from</code> 地址</span></a></h4>
<p>然而，如果应用使用相同的 <code v-pre>from</code> 地址，你每次发送邮件这种设置方式显得笨拙，替代的方法就是在 <code v-pre>config/mail.php</code> 配置文件中设置一个全局 <code v-pre>from</code> 地址，这个配置在没有单独指定 「from」时是默认的 <code v-pre>from</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'from'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'App Name'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置视图" tabindex="-1"><a class="header-anchor" href="#配置视图"><span>配置视图</span></a></h3>
<p>在 <code v-pre>build</code>方法内，你可以使用 <code v-pre>view</code> 方法指定邮件的模板，以渲染邮件的内容。因为所有邮件都会使用 <a href="https://learnku.com/docs/laravel/5.5/blade" target="_blank" rel="noopener noreferrer">Blade 模板</a> 渲染内容，你能很容易的使用 Blade 模板引擎构建邮件的 HTML：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Build the message.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} 你可以创建一个 <code v-pre>resources/views/emails</code> 目录来存放所有的邮件模板；然而，这不是强制要求，你可以在有的将邮件模板放在 <code v-pre>resources/views</code> 目录的任意位置。</p>
</blockquote>
<h4 id="纯文本邮件" tabindex="-1"><a class="header-anchor" href="#纯文本邮件"><span>纯文本邮件</span></a></h4>
<p>如果你想要定义一个纯文本邮件，你可以使用 <code v-pre>text</code> 方法。如同 <code v-pre>view</code> 方法，<code v-pre>text</code> 方法接受一个模板名称，这个名称告诉方法使用哪个模板来渲染邮件，可以自由定义邮件 HTML 和纯文本消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Build the message.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped_plain'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="视图数据" tabindex="-1"><a class="header-anchor" href="#视图数据"><span>视图数据</span></a></h3>
<h4 id="通过公共属性" tabindex="-1"><a class="header-anchor" href="#通过公共属性"><span>通过公共属性</span></a></h4>
<p>通常，你会希望传递一些数据来渲染你的邮件的 HTML 。那么有两种方法可以让视图获得数据，第一种，Mailable 类任何公共属性都可以在视图中使用。所以，例如你可以传递数据到 Mailable 类的构造函数并且在类中定义数据的公共属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * order 实例。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name">Order</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新消息实例。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Order</span> <span class="token variable">$order</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span> <span class="token operator">=</span> <span class="token variable">$order</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 构建消息。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> $this</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦数据被设置为公共属性，它们将自动在视图中加载，所以你可以访问像访问其他 Blade 模板数据一样访问它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div<span class="token operator">></span></span>
<span class="line">    Price<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$order</span><span class="token operator">-></span><span class="token property">price</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="通过-with-方法" tabindex="-1"><a class="header-anchor" href="#通过-with-方法"><span>通过 <code v-pre>with</code> 方法：</span></a></h4>
<p>你可以使用 <code v-pre>with</code> 方法来传递数据给模板。一般情况下，你仍然是使用 Mailable 类的构造函数来接受数据传参。然而你需要为这些数据属性设置 <code v-pre>protected</code> 或 <code v-pre>private</code> 声明，否则这些数据会被自动加载到模板中。接下来你可以使用 <code v-pre>with</code> 方法接受键值数组传参来传递数据给模板，就如控制器里为视图传参一样：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * order 实例。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name">Order</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新消息实例。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Order</span> <span class="token variable">$order</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span> <span class="token operator">=</span> <span class="token variable">$order</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 构建消息。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> $this</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                        <span class="token string single-quoted-string">'orderName'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">,</span></span>
<span class="line">                        <span class="token string single-quoted-string">'orderPrice'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">price</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦数据已经用 <code v-pre>with</code> 方法传递，它们将自动在视图中加载，所以你可以访问像访问其他 Blade 模板数据一样访问它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div<span class="token operator">></span></span>
<span class="line">    Price<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$orderPrice</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="附件" tabindex="-1"><a class="header-anchor" href="#附件"><span>附件</span></a></h3>
<p>要在邮件中加入附件，在 <code v-pre>build</code> 方法中使用 <code v-pre>attach</code> 方法。<code v-pre>attach</code> 方法接受文件的绝对路径作为它的第一个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Build the message.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> $this</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>附加文件到消息时，你也可以传递 <code v-pre>数组</code> 给 <code v-pre>attache</code> 方法作为第二个参数，以指定显示名称和 / 或是 MIME 类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Build the message.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> $this</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">                        <span class="token string single-quoted-string">'as'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                        <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="原始数据附件" tabindex="-1"><a class="header-anchor" href="#原始数据附件"><span>原始数据附件</span></a></h4>
<p><code v-pre>attachData</code> 可以使用字节数据作为附件。例如，你可以使用这个方法将内存中生成而没有保存到磁盘中的 PDF 附加到邮件中。<code v-pre>attachData</code> 方法第一个参数接收原始字节数据，第二个参数为文件名，第三个参数接受一个数组以指定其他参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Build the message.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> $this</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">attachData</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">pdf</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">                        <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="内部附件" tabindex="-1"><a class="header-anchor" href="#内部附件"><span>内部附件</span></a></h3>
<p>在邮件中嵌入内部图标通常是件麻烦事；然而 Laravel 提供了一个便利的方法，让你在邮件中附件图片并获取适当的 CID 。要嵌入内部图片，在邮件模板的 <code v-pre>$message</code> 变量上调用 <code v-pre>embed</code> 方法，Laravel 会自动让你所有邮件模板都能够获取到 <code v-pre>$message</code> 变量，所以不必担心如何手动传递它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>body<span class="token operator">></span></span>
<span class="line">    这是一张图片：</span>
<span class="line"></span>
<span class="line">    <span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string double-quoted-string">"{{ <span class="token interpolation"><span class="token variable">$message</span><span class="token operator">-></span><span class="token property">embed</span></span>(<span class="token interpolation"><span class="token variable">$pathToFile</span></span>) }}"</span><span class="token operator">></span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="嵌入原始数据附件" tabindex="-1"><a class="header-anchor" href="#嵌入原始数据附件"><span>嵌入原始数据附件</span></a></h4>
<p>如果你已经有想嵌入原始数据，希望嵌入邮件模板，你可以调用 <code v-pre>$message</code> 变量的 <code v-pre>embedData</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>body<span class="token operator">></span></span>
<span class="line">    这是一张原始数据图片：</span>
<span class="line"></span>
<span class="line">    <span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string double-quoted-string">"{{ <span class="token interpolation"><span class="token variable">$message</span><span class="token operator">-></span><span class="token property">embedData</span></span>(<span class="token interpolation"><span class="token variable">$data</span></span>, <span class="token interpolation"><span class="token variable">$name</span></span>) }}"</span><span class="token operator">></span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义-swiftmailer-消息" tabindex="-1"><a class="header-anchor" href="#自定义-swiftmailer-消息"><span>自定义 SwiftMailer 消息</span></a></h3>
<p><code v-pre>Mailable</code> 基类的 <code v-pre>withSwiftMessage</code> 方法允许你注册一个回调，该回调将在邮件发送之前调用，参数是原始 SwiftMailer 消息实例。这让你有机会在邮件发送之前自定义消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Build the message.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> $this</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">withSwiftMessage</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$message</span><span class="token operator">-></span><span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">addTextHeader</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Custom-Header'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'HeaderValue'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="markdown-格式的-mailable-类" tabindex="-1"><a class="header-anchor" href="#markdown-格式的-mailable-类"><span>Markdown 格式的 Mailable 类</span></a></h2>
<p>Markdown 格式的 Mailable 消息允许你从预编译的模板和你的 Mailable 类中的邮件提醒组件中受益。因为消息是用 Markdown 格式写的， Laravel 能为消息体渲染出漂亮、响应式的 HTML 模板，也能自动生成一个纯文本的副本。</p>
<h3 id="生成-markdown-格式的-mailable" tabindex="-1"><a class="header-anchor" href="#生成-markdown-格式的-mailable"><span>生成 Markdown 格式的 Mailable</span></a></h3>
<p>要生成一个包含友好的 Markdown 模板的 Mailable 类，你在使用 <code v-pre>make:mail</code> 这个 Artisan 命令时，要加上 <code v-pre>--markdown</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>mail OrderShipped <span class="token operator">--</span>markdown<span class="token operator">=</span>emails<span class="token operator">.</span>orders<span class="token operator">.</span>shipped</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后，在使用 <code v-pre>build</code> 方法配置 Mailable 时，用 <code v-pre>markdown</code> 方法来换掉 <code v-pre>view</code> 方法， <code v-pre>markdown</code> 方法接受一个 Markdown 模板的名称和一个将在模板中可用的选项数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="编写-markdown-格式的消息" tabindex="-1"><a class="header-anchor" href="#编写-markdown-格式的消息"><span>编写 Markdown 格式的消息</span></a></h3>
<p>Markdown Mailable 使用 Blade 组件和 Markdown 语法的组合，允许你轻松地构建邮件消息，同时利用 Laravel 的预制组件。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::message'</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token comment"># Order Shipped</span></span>
<span class="line"></span>
<span class="line">Your order has been shipped<span class="token operator">!</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">View Order</span>
<span class="line">@endcomponent</span>
<span class="line"></span>
<span class="line">Thanks<span class="token punctuation">,</span><span class="token operator">&lt;</span>br<span class="token operator">></span></span>
<span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.name'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="按钮组件" tabindex="-1"><a class="header-anchor" href="#按钮组件"><span>按钮组件</span></a></h4>
<p>按钮组件渲染一个居中的连接按钮，组件接受两个参数，一个 <code v-pre>url</code> 和一个可选的 <code v-pre>color</code> 。支持的颜色有 <code v-pre>blue</code> 、 <code v-pre>green</code> 、 和 <code v-pre>red</code> 。你可以在邮件消息体中加入随便多个你想要的按钮。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'green'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">View Order</span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="面板组件" tabindex="-1"><a class="header-anchor" href="#面板组件"><span>面板组件</span></a></h4>
<p>面板组件将面板中给定的一块文字的背景渲染成跟其他的消息体背景稍微不同。这样可以让这块文字引起人们的注意。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::panel'</span><span class="token punctuation">)</span></span>
<span class="line">This is the panel content<span class="token operator">.</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="表格组件" tabindex="-1"><a class="header-anchor" href="#表格组件"><span>表格组件</span></a></h4>
<p>表格组件允许你将一个 Markdown 格式的表格转换成一个 HTML 格式的表格。组件接受 Markdown 格式的表格作为它的内容。表格列对齐方式按照默认的 Markdown 对齐风格而定。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::table'</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">|</span> <span class="token class-name">Laravel</span>       <span class="token operator">|</span> <span class="token class-name">Table</span>         <span class="token operator">|</span> <span class="token class-name">Example</span>  <span class="token operator">|</span></span>
<span class="line"><span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span> <span class="token operator">|</span><span class="token punctuation">:</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token punctuation">:</span><span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token punctuation">:</span><span class="token operator">|</span></span>
<span class="line"><span class="token operator">|</span> <span class="token class-name">Col</span> <span class="token number">2</span> <span class="token class-name">is</span>      <span class="token operator">|</span> <span class="token class-name">Centered</span>      <span class="token operator">|</span> <span class="token variable">$10</span>      <span class="token operator">|</span></span>
<span class="line"><span class="token operator">|</span> <span class="token class-name">Col</span> <span class="token number">3</span> <span class="token class-name">is</span>      <span class="token operator">|</span> <span class="token class-name">Right</span><span class="token operator">-</span><span class="token class-name">Aligned</span> <span class="token operator">|</span> <span class="token variable">$20</span>      <span class="token operator">|</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义组件" tabindex="-1"><a class="header-anchor" href="#自定义组件"><span>自定义组件</span></a></h3>
<p>你或许会为了自定义而导出你应用中所有的 Markdown 邮件组件。要导出这些组件，使用 <code v-pre>vendor:publish</code> 这个 Artisan 命令来发布资源文件标签。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>laravel<span class="token operator">-</span>mail</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令会发布 Markdown 邮件组件到 <code v-pre>resources/views/vendor/mail</code> 文件夹。而 <code v-pre>mail</code> 文件夹会包含一个 <code v-pre>html</code> 文件夹和一个 <code v-pre>markdown</code> 文件夹，每个文件夹都包含他们的可用组件的描述。你可以按照你的意愿自定义这些组件。</p>
<h4 id="自定义样式表-css" tabindex="-1"><a class="header-anchor" href="#自定义样式表-css"><span>自定义样式表 CSS</span></a></h4>
<p>在导出组件之后，<code v-pre>resources/views/vendor/mail/html/themes</code> 文件夹将包含一个默认的 <code v-pre>default.css</code> 文件。你可以在这个文件中自定义 CSS ，你定义的这些样式将会在 Markdown 格式消息体转换成 HTML 格式时自动得到应用。</p>
<blockquote>
<p>{tip} 如果你想为 Markdown 组件构建一个全新的主题，只要写一个新的 CSS 文件，放在 <code v-pre>html/themes</code> 文件夹，然后在你的 <code v-pre>mail</code> 配置文件中改变 <code v-pre>theme</code> 选项就可以了。</p>
</blockquote>
<h2 id="在浏览器中预览邮件" tabindex="-1"><a class="header-anchor" href="#在浏览器中预览邮件"><span>在浏览器中预览邮件</span></a></h2>
<p>当你设计一个邮件模板时，可以很方便地在浏览器中快速预览渲染出来的邮件，就像典型的 Blade 模板一样。为此， Laravel 允许你直接从路由闭包或者控制器中返回任何的邮件。当邮件被返回时，他会被渲染并显示在浏览器中，允许你快速预览其设计，而不必发送到实际的电子邮件地址。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/mailable'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$invoice</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Invoice</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="发送邮件" tabindex="-1"><a class="header-anchor" href="#发送邮件"><span>发送邮件</span></a></h2>
<p>要发送邮件，使用 <code v-pre>Mail</code> <a href="https://learnku.com/docs/laravel/5.5/facades" target="_blank" rel="noopener noreferrer">facade</a> 的 <code v-pre>to</code> 方法。 <code v-pre>to</code> 方法接受一个邮件地址，一个 user 实现或一个 users 集合。如果传递一个对象或集合，mailer 将自动使用 <code v-pre>email</code> 和 <code v-pre>name</code> 属性来设置邮件收件人，所以确保你的对象里有这些属性。一旦指定收件人，你可以传递一个实现到 Mailable 类的 <code v-pre>send</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>OrderShipped</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Ship the given order.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name">Request</span>  <span class="token parameter">$request</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">int</span></span>  <span class="token parameter">$orderId</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">ship</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token variable">$orderId</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$order</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">findOrFail</span><span class="token punctuation">(</span><span class="token variable">$orderId</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// Ship order...</span></span>
<span class="line"></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，不局限于只使用「to」给收件人发送邮件，你可以通过一个单一的链式调用来自由的设置 「to」，「cc」和 「bcc」接收者：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="队列-mail" tabindex="-1"><a class="header-anchor" href="#队列-mail"><span>队列 Mail</span></a></h3>
<h4 id="将邮件消息加入队列" tabindex="-1"><a class="header-anchor" href="#将邮件消息加入队列"><span>将邮件消息加入队列</span></a></h4>
<p>由于发送消息会大幅延迟应用响应时间，许多开发者选择将邮件消息加入队列在后台进行发送。Laravel 使用内置的 <a href="https://learnku.com/docs/laravel/5.5/queues" target="_blank" rel="noopener noreferrer">统一的队列 API</a> 来轻松完成此工作。将邮件消息加入队列，使用 <code v-pre>Mail</code> facade 的 <code v-pre>queue</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这个方法会自动将工作加入队列，以便后台发送邮件。当然，在使用这个功能前，你需要 <a href="https://learnku.com/docs/laravel/5.5/queues" target="_blank" rel="noopener noreferrer">设置你的队列</a> 。</p>
<h4 id="延迟邮件消息队列" tabindex="-1"><a class="header-anchor" href="#延迟邮件消息队列"><span>延迟邮件消息队列</span></a></h4>
<p>如果你希望延迟发送已加入队列的邮件消息，你可以使用 <code v-pre>later</code> 方法。<code v-pre>later</code> 第一个参数接受一个 <code v-pre>DateTime</code> 实现以告知这个消息应该何时发送：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$when</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">Carbon<span class="token punctuation">\</span>Carbon</span><span class="token operator">::</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">later</span><span class="token punctuation">(</span><span class="token variable">$when</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="推送到特定队列" tabindex="-1"><a class="header-anchor" href="#推送到特定队列"><span>推送到特定队列</span></a></h4>
<p>因为所有 Mailable 类是通过 <code v-pre>make:mail</code> 命令生成并使用 <code v-pre>Illuminate\Bus\Queueable</code> trait ，你可以在任何 Mailable 类实现中调用 <code v-pre>onQueue</code> 来指定队列名称，还有 <code v-pre>onConnection</code> 方法来指定队列链接名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$message</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sqs'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onQueue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="默认队列" tabindex="-1"><a class="header-anchor" href="#默认队列"><span>默认队列</span></a></h4>
<p>如果你的 Mailable 类想要默认使用队列，你可以在类中实现 <code v-pre>ShouldQueue</code> 接口契约。现在，即便你调用 <code v-pre>send</code> 方法来发送邮件， Mailable 类仍将邮件放入队列中发送。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="邮件和本地开发" tabindex="-1"><a class="header-anchor" href="#邮件和本地开发"><span>邮件和本地开发</span></a></h2>
<p>当应用处于开发阶段在发送邮件时，你或许不想真实的发送邮件到真实的邮件地址。Laravel 提供几种方法以在本地开发时真实发送 「失去能力」。</p>
<h4 id="日志驱动" tabindex="-1"><a class="header-anchor" href="#日志驱动"><span>日志驱动</span></a></h4>
<p>代替真实发送，<code v-pre>log</code> 邮件驱动将所有邮件消息写入日志文件以供检查，需要更多根据环境来设置应用程序的信息，可参考 <a href="https://learnku.com/docs/laravel/5.5/configuration#environment-configuration" target="_blank" rel="noopener noreferrer">配置文件</a>.</p>
<h4 id="通用收件者" tabindex="-1"><a class="header-anchor" href="#通用收件者"><span>通用收件者</span></a></h4>
<p>另一个由 Laravel 提供的解决方案是设置一个通用邮件接收者，由框架发送邮件。这个方法将所有邮件发送到一个邮件地址，而不是发送给实际收件人。这可以通过 <code v-pre>config/mail.php</code> 配置文件的 <code v-pre>to</code> 选项来完成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'to'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Example'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="mailtrap" tabindex="-1"><a class="header-anchor" href="#mailtrap"><span>Mailtrap</span></a></h4>
<p>最后，你可以使用像 <a href="https://mailtrap.io/" target="_blank" rel="noopener noreferrer">Mailtrap</a> 和 <code v-pre>smtp</code> 驱动来将你的邮件消息发送到一个 「虚假的」邮箱中，你却可以在一个真实的邮件客户端中查看它们。这个方法的好处是让你可以在 Mailtrap 的消息阅读器中查看最终的实际邮件。</p>
<h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>Laravel 会在发送邮件消息之前触发一个事件。切记，这个事件只会在邮件 <em>发送</em> 时触发，在加入队列时不触发。你可以在你的 <code v-pre>EventServiceProvider</code> 注册一个事件监听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 应用事件监听映射。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$listen</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'Illuminate\Mail\Events\MessageSending'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'App\Listeners\LogSentMessage'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/qufo" target="_blank" rel="noopener noreferrer">@qufo</a></td>
<td><img src="https://avatars1.githubusercontent.com/u/2526883?v=3&amp;s=460?imageView2/1/w/100/h/100" alt="100"></td>
<td>翻译</td>
<td>欢迎共同探讨。<a href="https://github.com/qufo" target="_blank" rel="noopener noreferrer">@Qufo</a></td>
</tr>
<tr>
<td><a href="https://github.com/limxx" target="_blank" rel="noopener noreferrer">@limxx</a></td>
<td><img src="https://avatars0.githubusercontent.com/u/16585030?v=4&amp;s=400" alt="16585030?v=4&amp;s=400"></td>
<td>翻译</td>
<td>Winter is coming.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


